package com.zhengshang.quartz.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhengshang.api.domain.AppBillProduct;
import com.zhengshang.api.domain.AppThirdColumn;
import com.zhengshang.api.domain.AppThirdColumnCategory;
import com.zhengshang.api.service.IAppBillProductService;
import com.zhengshang.api.service.IAppThirdColumnCategoryService;
import com.zhengshang.api.service.IAppThirdColumnService;
import com.zhengshang.common.core.domain.AjaxResult;
import com.zhengshang.common.utils.ip.IpUtils;
import com.zhengshang.system.service.ISysConfigService;
import com.zhengshang.third.wikeyun.WikeyunApi;
import com.zhengshang.third.wikeyun.WikeyunUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.TreeMap;

/**
 * 微客云价格同步
 *
 * @author ruoyi
 */
@Component("weikeYunProductTask")
public class WeikeYunProductTask {

    protected final Logger logger = LoggerFactory.getLogger(WeikeYunProductTask.class);

    @Autowired
    private IAppThirdColumnService thirdColumnService;

    @Autowired
    private WikeyunUtils wikeyunUtils;

    @Autowired
    private IAppBillProductService appBillProductService;



    @Autowired
    private ISysConfigService configService;

    @Autowired
    private IAppThirdColumnCategoryService appThirdColumnCategoryService;



    /**
     * 微客云价格同步
     */
    public void syscWeikeYunProductPrice() {
        List<AppThirdColumn> appThirdColumnList = thirdColumnService.selectAppThirdColumnList(null);
        for (AppThirdColumn appThirdColumn : appThirdColumnList) {
            Long thirdColumnId = appThirdColumn.getThirdColumnId();
            Long wikeyunServerid = appThirdColumn.getWikeyunServerid();
            TreeMap<String, Object> params = new TreeMap<>();
            params.put("id", wikeyunServerid);
            String result = wikeyunUtils.sendPostRequest(params, WikeyunApi.getThirdProInfo);
            JSONObject jsonObject = JSONObject.parseObject(result);
            String code = jsonObject.getString("code");
            if ("0000".equals(code)) {
                JSONObject data = jsonObject.getJSONObject("data");
                JSONObject proInfo = data.getJSONObject("pro_info");
                JSONArray specJSONArray = proInfo.getJSONArray("spec");
                for (int i = 0; i < specJSONArray.size(); i++) {
                    JSONObject jsonObject1 = specJSONArray.getJSONObject(i);
                    JSONArray specList = jsonObject1.getJSONArray("spec_list");
                    for (int j = 0; j < specList.size(); j++) {
                        JSONObject jsonObject2 = specList.getJSONObject(j);
                        String spec_id = jsonObject2.getString("spec_id");
                        AppBillProduct appBillProduct = appBillProductService.selectWeiKeYunProduct(spec_id, thirdColumnId);
                        String times = jsonObject2.getString("times");
                        String productName = "";
                        if ("7天".equals(times)) {
                            productName = "周卡";
                        } else if ("1个月".equals(times)) {
                            productName = "月卡";
                        } else if ("3个月".equals(times)) {
                            productName = "季卡";
                        } else if ("6个月".equals(times)) {
                            productName = "半年卡";
                        } else if ("1年".equals(times)) {
                            productName = "年卡";
                        } else {
                            productName = times;
                        }
                        if (ObjectUtils.isNotEmpty(appBillProduct)) {
                            //给平台用户的成本价
                            BigDecimal cmsPrice = jsonObject2.getBigDecimal("cms_price");

                            //成本价
                            appBillProduct.setMobileMoneyCost(cmsPrice);
                            BigDecimal official_price = jsonObject2.getBigDecimal("official_price");
                            appBillProduct.setMoney(official_price);

                            appBillProduct.setMobileFuluProductName(productName);
                            BigDecimal proportion = new BigDecimal(configService.selectConfigByKey("app.proportion","1.05"));
                            appBillProduct.setMobileMoneyPrice(cmsPrice.multiply(proportion).setScale(1, BigDecimal.ROUND_HALF_UP));
                            appBillProductService.updateAppBillProduct(appBillProduct);
                        }else{

                            //给平台用户的成本价
                            BigDecimal cmsPrice = jsonObject2.getBigDecimal("cms_price");
                            String productImg = jsonObject2.getString("spec_img");
                            //划线价
                            BigDecimal officialPrice = jsonObject2.getBigDecimal("official_price");
                            AppBillProduct newAppBillProduct = new AppBillProduct();
                            newAppBillProduct.setProductType(appThirdColumn.getThirdColumnType());
                            newAppBillProduct.setMobileFuluProductName(productName);
                            String desc = jsonObject2.getString("title") + "_" + times;
                            newAppBillProduct.setProductDesc(desc);
                            newAppBillProduct.setProductImg(productImg);
                            //成本价
                            newAppBillProduct.setMobileMoneyCost(cmsPrice);
                            //价格
                            newAppBillProduct.setMoney(cmsPrice.add(new BigDecimal(3)));
                            //划线价
                            newAppBillProduct.setMobileMoneyPrice(officialPrice);
                            newAppBillProduct.setProductSort(j + 1);
                            newAppBillProduct.setMobileFuluProductId(spec_id);
                            newAppBillProduct.setThirdColumnId(thirdColumnId);
                            String specTitle = jsonObject1.getString("spec_title");
                            AppThirdColumnCategory appThirdColumnCategory = appThirdColumnCategoryService.selectAppThirdColumnCategoryByName(specTitle);
                            if (ObjectUtils.isEmpty(appThirdColumnCategory)) {
                                appThirdColumnCategory = new AppThirdColumnCategory();
                                appThirdColumnCategory.setThirdColumnId(thirdColumnId);
                                appThirdColumnCategory.setThirdColumnCategoryName(specTitle);
                                appThirdColumnCategory.setThirdColumnCategorySort(i + 1);
                                appThirdColumnCategoryService.insertAppThirdColumnCategory(appThirdColumnCategory);
                            }
                            Long thirdColumnCategoryId = appThirdColumnCategory.getThirdColumnCategoryId();
                            newAppBillProduct.setThirdColumnCategoryId(thirdColumnCategoryId);
                            appBillProductService.insertAppBillProduct(newAppBillProduct);
                        }
                    }
                }
            }
        }
    }
}
